package com.vitalfit.service.serviceImpl;

import com.vitalfit.service.NotificationObserver;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationObserverImpl implements NotificationObserver {

    private final JavaMailSender emailSender;

    @Override
    public void sendBulkEmails(List<String> emailAddresses, String subject, String message) {
        // 创建一个邮件消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("jinhang.shi02@gmail.com");
        mailMessage.setSubject(subject);
        mailMessage.setText(message);

        // 由于SimpleMailMessage只支持单个接收者，我们需要为每个邮箱地址创建和发送一个邮件消息
        emailAddresses.forEach(emailAddress -> {
            mailMessage.setTo(emailAddress);
            try {
                emailSender.send(mailMessage);
                log.error("Email sent to: " + emailAddress);
            } catch (Exception e) {
                log.error("Failed to send email to: " + emailAddress);
            }
        });
    }

    @Override
    public void sendBulkSMS(List<String> phoneNumber, String subject, String message) {

    }
}

